<?php
/** 备忘录类 */
class Memento
{
    protected $state;
    public function __construct($state)
    {
        $this->state = $state;
    }
    public function getState()
    {
        return $this->state;
    }
}
/** 需要被记录的类 */
class Originator
{
    protected $state;
    public function setState($state)
    {
        $this->state = $state;
        return $this;
    }
    public function getState()
    {
        return $this->state;
    }
    public function show()
    {
        echo $this->state . "\r\n";
    }
    public function save()
    {
        return new Memento($this->state);
    }
    /** @param Memento $memento */
    public function load($memento)
    {
        $this->state = $memento->getState();
    }
}
/** 管理备忘录的类 */
class CareTaker
{
    protected $list=[];
    /** @param Memento $memento */
    public function add($memento)
    {
        $this->list[] = $memento;
        return array_search($memento, $this->list);
    }
    /** @return Memento */
    public function get($index){
        return $this->list[$index];
    }
}

header('Content-Type: text/plain');

$obj = new Originator;
$ct = new CareTaker;

$obj->setState("hello");
$obj->show();
$id = $ct->add($obj->save());

$obj->setState("regerhgwreger");
$obj->show();

$obj->load($ct->get($id));
$obj->show();
